<?php

class Login extends MX_Controller 
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		
		$this->load->model('user/model_user');
		if($this->model_user->is_logged_in())
		{
			//echo "1<br>";
			redirect('user/home');
		}
		else
		{
			//echo "2<br>";
			if($this->input->server('REQUEST_METHOD') == 'POST')
			{
				//echo "3<br>";
				$this->form_validation->set_rules('username', 'lang:login_undername', 'callback_login_check');
				$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
				
				if($this->form_validation->run($this) == FALSE)
				{
					//echo "4<br>";
					$this->load->view('user/login');
				}
				else
				{
					//echo "5<br>";
					//exit;
					redirect('user/home');
				}
			}
			else
			{
				//echo "6<br>";
				$this->load->view('user/login');
			}
		}
	}
	
	function login_check($username)
	{
		$password = $this->input->post("password");
		
		if(!$this->model_user->login($username,$password))
		{
			$this->form_validation->set_message('login_check', $this->lang->line('login_invalid_username_and_password'));
			return false;
		}
		return true;		
	}
}
?>